package com.iengoo.spacewar;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

import static com.iengoo.spacewar.Config.START_TIME;
import static com.iengoo.spacewar.Config.STATUS;

public class Space extends JPanel implements KeyListener {
//    private MineContainer mineList = MineContainer.getInstance();
    private GridContainer gridList = GridContainer.getInstance();
    private Craft craft;

    /**
     * 背景贴图
     */
    private BufferedImage bg;

    public Space() throws IOException {
        bg = ImageIO.read(getClass().getResource("resources/backdrop.png"));
        init();
        addKeyListener(this);
        addMouseMotionListener(new TGMouseListener());
        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Point point = e.getPoint();
                if (point.getY() > 310 && point.getY() < 340
                        && point.getX() > 140 && point.getX() < 230) {
                    Config.START_TIME = System.currentTimeMillis();
                    Config.CURRENT_GAME_LEVEL = GameLevel.LEVEL_01;
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        initThread();
    }

    /**
     * 初始化游戏界面
     *
     * @throws IOException
     */
    public void init() throws IOException {
        craft = new Craft();
        GridContainer gridList = GridContainer.getInstance();
        // x代表行
        // y代表列

        for (int x = 0; x < GridContainer.numX; x++) {
            for (int y = -1; y < GridContainer.numY; y++) {
                try {
                    gridList.add(new Grid(Grid.WIDTH * x, Grid.HEIGHT * y));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void initThread() {
        WorkerThread worker = new WorkerThread();
        worker.init(craft, this);
        // 启动地雷生产线程
        worker.start();
    }

    @Override
    public void paint(Graphics g) {
        // 画背景图
        g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);

        // 绘制背景滚动格子图片
        for (int i = 0; i < gridList.size(); i++) {
            gridList.get(i).paint(g);
        }

//        for (int i = 0; i < mineList.size(); i++) {
//            mineList.get(i).paint(g);
//        }
//
//        for (Plasma p : craft.getPlasmaList()) {
//            p.paint(g);
//        }
//
//        // 绘制爆炸效果
//        BombContainer bc = BombContainer.getInstance();
//        for (int i = 0; i < bc.size(); i++) {
//            bc.get(i).paint(g);
//        }
//
//        craft.paint(g);
        LevelManager.getLevelManager(craft).runGame(g);
    }


    public void action() {
        STATUS = GameStatus.START;
        START_TIME = System.currentTimeMillis();
        while (true) {
            repaint();
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        String keyText = null;
        Character keyChar = null;
        switch (e.getKeyCode()) {
            case KeyEvent.VK_ESCAPE: keyText = "ESC"; break;
            case KeyEvent.VK_TAB: keyText = "TAB"; break;
            case KeyEvent.VK_CAPS_LOCK: keyText = "CAPS_LK"; break;
            case KeyEvent.VK_SHIFT: keyText = "SHIFT"; break;
            case KeyEvent.VK_CONTROL: keyText = "CTRL"; break;
            case KeyEvent.VK_SPACE: keyText = "BLANK"; break;
            case KeyEvent.VK_ENTER: keyText = "ENTER"; break;
            case KeyEvent.VK_ALT: keyText = "ALT"; break;
            case KeyEvent.VK_BACK_SPACE: keyText = "BACK"; break;
            case KeyEvent.VK_DELETE: keyText = "DELETE"; break;
            default: keyChar = e.getKeyChar();
        }

        if (keyText != null) {
            MessageQueue.getInstance().push(keyText);
        }
        if (keyChar != null) {
            MessageQueue.getInstance().push(keyChar);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {}
}

 class TGMouseListener implements MouseMotionListener {
     @Override
     public void mouseDragged(MouseEvent e) {

     }

     @Override
     public void mouseMoved(MouseEvent e) {
         Point point = e.getPoint();
         if (point.getY() > 310 && point.getY() < 340
                 && point.getX() > 140 && point.getX() < 230) {
             MessageQueue.getInstance().push("EnterStartGameZone");
         } else {
             MessageQueue.getInstance().push("OutOfStartGameZone");
         }
     }
 }
